package com.chanyachting.emall.action.businesstype;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Businesstype;
import com.chanyachting.emall.form.BusinesstypeForm;
import com.chanyachting.emall.service.BusinesstypeService;
import com.chanyachting.emall.serviceImpl.BusinesstypeServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class UpdateAction extends Action {
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		if(request.getMethod().equals("POST")){
			
			BusinesstypeForm btForm=(BusinesstypeForm)form;
			
			if(btForm.validate(mapping, request).isEmpty()){
				
				Businesstype by=new Businesstype();
				
				BeanUtils.copyProperties(by, btForm);
				
				BusinesstypeService bs=new BusinesstypeServiceImpl();
				
				bs.updateBusinesstype(by);
				
				new RequestUtil(request).setSuccessMsg();
				
			}
			
		}
			
		//重新加载 businesstype 对象
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		BusinesstypeService bs=new BusinesstypeServiceImpl();
		
		Businesstype by=new Businesstype();
		
		by.setId(id);
		
		by=bs.getOneBusinesstype(by);
		
		new RequestUtil(request).setAttribute("businessType", by);
			

		
		return mapping.findForward("businesstypeUpdate");
		
	}
	

}
